import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { environment } from '@env/environment';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { DragModalService } from '../../../../shared/utils/drag-modal.service';
import { TallySearchExportService } from '../tally-search-export.service';

@Component({
	selector: 'app-tally-search-export-tst-list',
	templateUrl: './tst-list.component.html',
	styleUrls: ['./tst-list.component.less']
})
export class TallySearchExportTstListComponent implements OnInit {
	@Input()
	searchParams: any;
	@Input()
	preCompany: any;
	pageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	pageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};
	@ViewChild('st', { static: false }) private st!: STComponent;
	columns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.st.pi - 1) * this.st.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '安全生产',
			fixed: 'left',
			index: 'isSafeProduction',
			render: 'isSafeProduction',
			width: 80
		},
		{
			title: '日期班次',
			fixed: 'left',
			index: 'timeInfo',
			width: 150
		},
		{
			title: '来源船',
			fixed: 'left',
			index: 'oriShipInfo',
			width: 150
		},
		{
			title: '来源单号',
			fixed: 'left',
			index: 'oriBillNo',
			width: 150
		},
		{
			title: '来源货名',
			index: 'oriBillCargoTypeName',
			width: 80
		},
		{
			title: '去向船',
			index: 'toShipInfo',
			width: 150
		},
		{
			title: '去向单号',
			index: 'toBillNo',
			width: 150
		},
		{
			title: '去向货名',
			index: 'toBillCargoTypeName',
			width: 80
		},
		{
			title: '航陆运',
			index: 'shipLandName',
			width: 60
		},
		{
			title: '舱口',
			index: 'holdName',
			width: 60
		},
		{
			title: '理货类型',
			index: 'tallyTypeName',
			width: 80
		},
		{
			title: '作业过程',
			index: 'operationTypeName',
			width: 100
		},
		{
			title: '包装',
			index: 'packageInfo',
			width: 80
		},
		{
			title: '作业前',
			index: 'oriPositionInfo',
			width: 120
		},
		{
			title: '作业后',
			index: 'toPositionInfo',
			width: 120
		},
		{
			title: '理货件',
			index: 'pieceInfo',
			width: 100
		},
		{
			title: '理货吨',
			index: 'tonInfo',
			width: 150
		},
		{
			title: '调吨类型',
			index: 'checkTypeName',
			width: 80
		},
		{
			title: '检算件',
			index: 'checkPieceInfo',
			width: 100
		},
		{
			title: '检算吨',
			index: 'checkTonInfo',
			width: 150
		},
		{
			title: '装卸队组',
			index: 'workerInfo',
			width: 120
		},
		{
			title: '作业机械',
			index: 'machineInfo',
			width: 120
		},
		{
			title: '备注',
			index: 'remark',
			width: 80
		},
		{
			title: '理货员',
			fixed: 'right',
			index: 'createUserName',
			className: 'text-center',
			width: 80
		},
		{
			title: '保管员',
			fixed: 'right',
			index: 'keeperName',
			className: 'text-center',
			width: 80
		}
	];
	dataList = [];
	totalData: any;

	constructor(
		private modalService: DragModalService,
		private tallySearchExportService: TallySearchExportService
	) {
	}

	ngOnInit(): void {
		this.loadData();
	}

	loadData(): any {
		const params = {
			preCompany: this.preCompany,
			...this.searchParams,
			size: this.pageInfo.size,
			current: this.pageInfo.current
		};
		this.pageInfo.loading = true;
		this.tallySearchExportService.tstTallySearchExportPage(params).subscribe((res: any) => {
			this.dataList = res.records;
			this.pageInfo.total = res.total;
			this.pageSetting.total = res.total;
			this.pageInfo.loading = false;
			this.tallySearchExportService.tstTallySearchExportTotal(params).subscribe((total: any) => {
				this.totalData = total;
			});
		});
	}

	resetParams(): void {
		this.pageInfo.current = 1;
		for (let key of Object.keys(this.searchParams)) {
			this.searchParams[key] = null;
		}
	}

	stChange(ret: STChange) {
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.pageInfo.current = ret.pi;
			this.pageInfo.size = ret.ps;
			this.loadData();
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};
}
